package se.webbzon.boltzmann.minigame.machine;

import java.util.ArrayList;
import java.util.Collection;

import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxIcon;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxObject;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SandBoxMachine extends DefaultSandBoxItem {
	
	// The 2-dimensional depth of this sand box item
	private static final int depth2d = 100;
	
	// The size of the machine
	private static final int width = 32, height = 32;
	
	// The parts of this machine
	private Collection<MachinePart> parts;
	
	/*============================================================
	Constructors
	============================================================*/

	public SandBoxMachine() {
		parts = new ArrayList<MachinePart>();
		setInstance(new Instance());
		setSize(width,height);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Adds a machine part to this machine. **/
	public boolean addPart(MachinePart part) {
		return parts.add(part);
	}
	
	/** Removes a machine part from this machine. **/
	public boolean removePart(MachinePart part) {
		return parts.remove(part);
	}
	
	/** Clears all machine parts from this machine. **/
	public void clearParts() {
		parts.clear();
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Instance extends SandBoxObject {

		public Instance() {
			super(WorldObject.onStepEnd + WorldObject.onPostDraw, depth2d);
		}

		@Override public void onAttach(SandBox box) {};

		@Override public void onDetach(SandBox box) {};

		@Override public void onStepEnd(WorldArea world) {
			super.onStepEnd(world);
			for (MachinePart part : parts)
				part.onStepEnd();
		}

		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			int hw = width/2, hh = height/2;
			int xm = (int) getX()-hw, ym = (int) getY()-hh;
			for (MachinePart part : parts) {
				int x = xm + (int) part.getX();
				int y = ym + (int) part.getY();
				hud.drawTexture(part.getTexture3D(), x, y, width, height);
			}
		}

		@Override public SandBoxIcon createIcon() {
			return null;
		}
		
	}
	
}
